package cn.pzhu.services.admin;

import cn.pzhu.model.Achievement;
import cn.pzhu.model.Demand;
import cn.pzhu.services.BaseServices;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dongp on 2016/4/27.
 */
public class DemandServices extends BaseServices{

    public List ajaxQuery(String pageNumber, String pageSize, String search) {
        ArrayList arrts = new ArrayList();
        Map<String,Object> map = new HashMap<>();

        List<Demand> demands = Demand.dao.find("SELECT t2.*,d.dName statusName FROM" +
                " (SELECT t1.*,c.typeName FROM" +
                " (SELECT a.*,u.account FROM issuedemand a " +
                " LEFT JOIN user u ON a.userID=u.userID) t1  " +
                " LEFT JOIN category c ON t1.categoryID = c.typeVal) t2" +
                " LEFT JOIN dict d ON t2.status = d.dVal" +
                " where name like '%" + search + "%' limit " + pageNumber + "," + pageSize);
        arrts.add(demands);

        Demand demand = Demand.dao.findFirst("SELECT count(*) total FROM" +
                " (SELECT t2.*,d.dName statusName FROM" +
                " (SELECT t1.*,c.typeName FROM" +
                " (SELECT a.*,u.userName FROM issuedemand a " +
                " LEFT JOIN user u ON a.userID=u.userID) t1  " +
                " LEFT JOIN category c ON t1.categoryID = c.typeVal) t2" +
                " LEFT JOIN dict d ON t2.status = d.dVal) t4 " +
                " where t4.name like '%"+search+"%'");
        Long total = demand.getLong("total").longValue();
        map.put("total", total);
        arrts.add(map);
        return arrts;
    }

    public boolean ajaxUpdate(int id, int status){
        boolean result = Demand.dao.findById(id).set("status", status).update();
        return result;
    }

}
